<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
<script>
        const input = document.querySelector('input')
        const sendMsg = function(){
            console.log('发送请求')
            console.log(this)
            // 不用防抖的情况，原本sendMsg里面的this指向的是input
        }

        const debounce = (fn, ms = 0) => {
            let timerId
            return function(){
                clearTimeout(timerId)
                timerId = setTimeout(() => {
                    fn.call(this)
                }, ms)
            }
        }

        // input.addEventListener('keyup', sendMsg)
        // 当我们使用debounce防抖函数来控制sendMsg的执行时，改变了sendMsg里的this的指向
        input.addEventListener('keyup', debounce(sendMsg, 300))
        input.addEventListener('keyup', function(){
                clearTimeout(timerId)
                // 这里的this 指向的是 input 
                timerId = setTimeout(() => {
                    console.log(this) // input 
                    fn.call(this)
                    // 想要fn调用执行的时候，fn内部的this指向input 
                }, ms)
            })
</script>
</body>
</html>